// const fs = require('fs')
// const os = require('os')
// const path = require('path')
import fs from 'fs'
import os from 'os'
import path from 'path'

let netWorkInterfaces = os.networkInterfaces()
let ip = ''

Object.values(netWorkInterfaces).forEach(list => {
  list.forEach(item => {
    if(item.family === 'IPv4' && item.address !== '127.0.0.1' && !item.internal){
      // ip = item.address
      ip = '192.168.10.171'
    }
  })
})

let filPpathUAT = path.join(path.resolve(),'./env/.env.uat')
let filPpathPRODUCT = path.join(path.resolve(),'./env/.env.production')
let str = fs.readFileSync(filPpathUAT,'utf-8')

// // 匹配文件中的ip地址
let reg = new RegExp(`(?<=VITE_API_URL\\s*=\\s*'(http|https):\\/\\/)(.*)(?=:)`)
let newstr = str.replace(reg,ip)

if (str !== newstr) {
  // 写入uat文件
  try {
    fs.writeFileSync(filPpathUAT,newstr)
    fs.writeFileSync(filPpathPRODUCT,newstr)
    console.log('ip已变更为:'+ip);
  } catch (err) {
    console.log(err);
  }
}
